// pages/danger/index.js
var app = getApp()
var util = require('../../utils/util.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    scroll: true,
    list: [{
        id: 'deal',
        title: '我的待办'
      },
      {
        id: 'self',
        title: '隐患自查'
      },
      {
        id: 'plan',
        title: '年度计划'
      },
    ],
    selectedId: 'deal',
    height: 50,
    fixed: false,
    inspectId: 0,
    selfInspectPublicList: [],
    active: 0,
    page: 1,
    reachBottom: false, //表示已经没有跟多的数据了
    dangerTaskCountInfo: {
      reform: 0, //待整改
      checkAccept: 0, //待验收
      unCheckCount: 0, //带检查
      reformSheet: 0
    },
    targetDeptId: 0,
    targetCompanyId: 0,
  },
  scanQrCode: function(e) {
    if (e.currentTarget.dataset.type == 2) {
      wx.navigateTo({
        url: '/pages/danger/companySelectors/index',
      })
    } else {
      wx.scanCode({
        success: (res) => {
          console.log(res)
          var qrCodeJson = JSON.parse(res.result)
          this.setData({
            inspectId: res.result
          })
          var companyId = 0;
          if (e.currentTarget.dataset.type == 0 || e.currentTarget.dataset.type == 1) {
            companyId = app.globalData.user.companyId
          } else {
            companyId = qrCodeJson.companyId
          }
          //TODO
          wx.navigateTo({
            url: '/pages/danger/selfAdd/index?inspectId=' + qrCodeJson.inspectId + '&inspect=true' +
              '&type=' + e.currentTarget.dataset.type + '&deptId=' + qrCodeJson.deptId +
              '&deptName=' + qrCodeJson.deptId + '&companyId=' + companyId +
              '&companyName=' + qrCodeJson.companyName,
          })
        }
      })
    }

  },
  /**
   * 进入发布的隐患自查
   */
  inspectPublic: function(e) {
    var inspectPublicId = e.currentTarget.dataset.inspectpublicid
    wx.navigateTo({
      url: '/pages/danger/myHistoryInspect/index?inspectId=' + inspectPublicId + '&inspect=false',
    })
  },
  handleTabChange(e) {
    this.setData({
      active: e.detail.index
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // var that = this
    this.loadSelfInspectPublic()
    this.loadDangerTaskInfo()
  },

  /**
   * 加载当前用户的自查列表
   */
  loadSelfInspectPublic() {
    var that = this
    var header;
    header = {
      'Cookie': wx.getStorageSync("sessionid") //读取cookie
    };
    var params = {
      createUserId: app.globalData.user.userId,
      // type: 0,
      status: 15
    }
    wx.request({
      header: header,
      url: app.globalData.baseUrl + '/safe/danger/inspectPublic/find',
      method: "POST",
      data: params,
      success(res) {
        console.log(res)
        var selfInspectPublicList = res.data.data.list
        for (var i = 0; i < selfInspectPublicList.length; i++) {
          selfInspectPublicList[i].createTimeStr = util.formatTime(new Date(selfInspectPublicList[i].createTime))
          selfInspectPublicList[i].statusName = that.getInspectStatusName(selfInspectPublicList[i].status)
        }
        that.setData({
          selfInspectPublicList: selfInspectPublicList
        })
      }
    })
  },

  /**
   * 加载隐患的任务数量
   */
  loadDangerTaskInfo() {
    var that = this
    var header;
    wx.showLoading({
      title: '',
    })
    header = {
      'Cookie': wx.getStorageSync("sessionid") //读取cookie
    };
    wx.request({
      header: header,
      url: app.globalData.baseUrl + '/safe/wechat/dangerTaskTypeCount',
      method: "GET",
      success(res) {
        wx.hideLoading()
        if (res.data.success) {
          that.setData({
            dangerTaskCountInfo: res.data.data
          })
        }
      }
    })
  },
  /**
   * 根据状态值获取状态名
   */
  getInspectStatusName(status) {
    var statusName = ""
    switch (status) {
      case 1:
        statusName = "审核"
        break;
      case 2:
        statusName = "修改检查单"
        break;
      case 3:
        statusName = "待检查"
        break;
      case 4:
        statusName = "发布整改单"
        break;
      case 5:
        statusName = "隐患评估"
        break;
      case 6:
        statusName = "审核"
        break;
      case 7:
        statusName = "整改"
        break;
      case 8:
        statusName = "整改审核"
        break;
      case 9:
        statusName = "验收"
        break;
      case 10:
        statusName = "督办整改"
        break;
      case 11:
        statusName = "督办整改审核"
        break;
      case 12:
        statusName = "督办验收"
        break;
      case 13:
        statusName = "督办验收审核"
        break;
      case 14:
        statusName = "整改验收审核"
        break;
      case 15:
        statusName = "结束"
        break;
      default:
        statusName = "未知状态"
    }
    return statusName
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.loadDangerTaskInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    // 显示顶部刷新图标
    wx.showNavigationBarLoading();
    var that = this
    var header;
    header = {
      'Cookie': wx.getStorageSync("sessionid") //读取cookie
    };
    var params = {
      createUserId: app.globalData.user.userId,
      type: 0,
    }
    wx.request({
      header: header,
      url: app.globalData.baseUrl + '/safe/danger/inspectPublic/find',
      method: "POST",
      data: params,
      success(res) {
        console.log(res)
        var selfInspectPublicList = res.data.data.list
        for (var i = 0; i < selfInspectPublicList.length; i++) {
          selfInspectPublicList[i].createTimeStr = util.formatTime(new Date(selfInspectPublicList[i].createTime))
          selfInspectPublicList[i].statusName = that.getInspectStatusName(selfInspectPublicList[i].status)
        }
        that.setData({
          selfInspectPublicList: selfInspectPublicList
        })
        // 隐藏导航栏加载框
        wx.hideNavigationBarLoading();
        // 停止下拉动作
        wx.stopPullDownRefresh();

      }
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    // 显示加载图标
    wx.showLoading({
      title: '玩命加载中',
    })
    var header;
    header = {
      'Cookie': wx.getStorageSync("sessionid") //读取cookie
    };
    var params = {
      createUserId: app.globalData.user.userId,
      // type: 0,
      pageCurrent: this.data.page + 1,
      status: 15
    }
    wx.request({
      header: header,
      url: app.globalData.baseUrl + '/safe/danger/inspectPublic/find',
      method: "POST",
      data: params,
      success(res) {
        console.log(res)
        wx.hideLoading()
        if (that.data.reachBottom) {
          wx.showToast({
            title: '没有更多数据',
          })
        } else {
          var newSelfInspectPublicList = res.data.data.list
          var oldSelfInspectPublicList = that.data.selfInspectPublicList
          for (var i = 0; i < newSelfInspectPublicList.length; i++) {
            newSelfInspectPublicList[i].createTimeStr = util.formatTime(new Date(newSelfInspectPublicList[i].createTime))
            newSelfInspectPublicList[i].statusName = that.getInspectStatusName(newSelfInspectPublicList[i].status)
            oldSelfInspectPublicList.push(newSelfInspectPublicList[i])
          }
          that.setData({
            selfInspectPublicList: oldSelfInspectPublicList,
            page: that.data.page + 1
          })
          if (res.data.data.isLastPage) {
            that.setData({
              reachBottom: true
            })
          }
        }
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})